//Demo
layui.use('form', function(){
    var form = layui.form;

    //监听提交
    form.on('submit(formDemo)', function(data){
        layer.msg(JSON.stringify(data.field));
        return false;
    });
});
$(function () {
    showPage();
    showData();
    $("#query").click(function () {
        showData();
        showPage();
    })
})
//显示数据
function showData() {
    var hospital_id=$("#buiding").val();
    $.ajax({
        "url": "/getPage",
        "type": "get",
        "async": false,
        "data": {"pageNum": pageNum, "pageSize": pageSize,"user_id": user_id},
        "dataType": "json",
        "success": function (res) {
            console.log(res.currPageNo)
            console.log(res.totalCount)
            console.log("res.totalCount ===== >"+res.totalCount)
            pageNum = res.currPageNo;//当前页码
            totalCount = res.totalCount;
            let html = '';
            let list = res.accessControlDeviceList;
            console.log(res.AccessControlDeviceList)
            for (let i = 0; i < list.length; i++) {
                html += '<tr>';
                html += '<td>' + list[i].count + '</td>';
                html += '<td>' + list[i].operationtype + '</td>';
                html += '<td>' + list[i].operationtime + '</td>';
                html += '<td>' + list[i].balance + '</td>';
                console.log(list[i].user_id)
                html += '<td>' + list[i].user.id + '</td>';
                html += '<td>' + timestampToString(list[i].operationtime) + '</td>';
                if (list[i].operationtype==0){
                    html += '<td>开盲盒</td>';
                }else{
                    html += '<td>邀请用户</td>';
                }
            }
            //alert(html);
            //将拼接好的内容写入到tbody里面
            $("tbody").empty().html(html);
        },
        "error": function () {
            alert("请求失败");
        }
    });
}
let pageNum = 1;//设置当前页码
let pageSize = 5;//设置每页显示的条数0
let totalCount;//总条数0
let totalPage;//总共多少页
//显示面码的方法
function showPage() {
    layui.use(['laypage'], function () {
        var laypage = layui.laypage;
        laypage.render({
            elem: 'pages'   //这里的jg是放该页码标签的id，不需要加#
            , count: totalCount   //总数量
            , page: pageNum   //当前页码
            , limit: pageSize   //页面尺寸
            , layout: ['prev', 'page', 'next', 'skip', 'count', 'limit']
            , jump: function (obj, first) {
                pageNum = obj.curr;
                pageSize = obj.limit;
                //首次不执行，一定要加此判断，否则初使时会无限刷新
                if (!first) {
                    showData();
                }
            }
        })
    });
}

function timestampToString(timstamp) {
    var date = new Date(timstamp).toJSON();
    return newDate = new Date(+new Date(date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
}

$(function (){
    $("#addUser").click(function (){
        layui.use('layer', function(){
            layer.open({
                type: 2,
                area: ['500px','520px'],
                anim: 1,
                content: ['/user/add' , 'no'],
            });
        });
    })
})

function  upd(id,status){
        layui.use('layer', function () {
            var layer = layui.layer;
            $.ajax({
                "url": "/updStatus",
                "type": "get",
                "data": {
                    "id": id,
                    "status":status
                },
                "dataType": "text",
                "success": function (result) {
                    layer.confirm('确认要修改设备状态吗？', {icon: 0, title: '提示信息'}, function (index) {
                        if (result.indexOf("true") > -1) {
                            layer.msg('修改成功!', {
                                icon: 1,
                                time: 1000 //2秒关闭（如果不配置，默认是3秒）
                            }, function () {
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index)
                            });
                        } else {
                            layer.msg('修改失败!', {icon: 2});
                        }
                    })
                },
                "error": function () {
                    alert("出现异常")
                }
            });
        })
}

$(function (){
    $("#add").click(function (){
        layui.use('layer', function(){
            layer.open({
                type: 2,
                area: ['500px','520px'],
                anim: 1,
                content: ['/add' , 'no'],
            });
        });
    })
})